Generating QR code for a link
Creating an QR code for a link
-
Before you begin, create a Secret API key by following the guidelines in this article.
-
Get the link ID of the short link you want to edit (follow the steps in this article).
-
Install prerequisites for requests:
- Python
- Node.js
pip install requests
npm install @short.io/client-node
Now everything is ready to run the following snippet. It will create a region targeting rule for a short URL.
- Create a file: filename.py/ .js/ .rb. Use the code snippet below.
Please, replace link_idString, API_KEY, FILE_NAME with an appropriate value.
Consider the following parameters:
| Parameter name | Type | Constraints | Description |
|---|---|---|---|
| type | string | optional | File format: png, svg, pdf |
| color | number | optional | 0xff0000 |
| backgroundColor | number | optional | 0xff0000 |
| size | number | optional | 6 |
- Python
- Node.js
- Ruby
import requests
url = "https://api.short.io/links/qr/{link_idString}"
import json
payload = json.dumps({"type":"png"})
headers = {
'accept': "application/json",
'content-type': "application/json",
'authorization': "API_KEY"
}
response = requests.request("POST", url, data=payload, headers=headers)
open('FILE_NAME.png', 'wb').write(response.content)
import { setApiKey, generateQrCode } from "@short.io/client-node";
setApiKey("API_KEY");
const result = await generateQrCode({
path: { linkId: "link_idString" },
body: { type: "png" }
});
console.log(result.data);
require 'uri'
require 'net/http'
require 'openssl'
require 'json'
url = URI("https://api.short.io/links/qr/{link_idString}")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(url)
request["accept"] = 'application/json'
request["content-type"] = 'application/json'
request["authorization"] = 'API_KEY'
request.body = JSON.generate({"type": "png"})
response = http.request(request)
puts response.read_body
- Launch the file.
- Python
- Node.js
- Ruby
python filename.py
node filename.js
ruby filename.rb